package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.FeaturesModel;
import application.model.modify.FeatureModel;
import application.translators.Translator;
import application.translators.impl.FeaturesTranslator;
import dao.FeaturesDao;
import mappings.Features;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("features")
public class FeaturesService
{
    private FeaturesDao featuresDao = new FeaturesDao();
    private Translator<List<Features>, FeaturesModel> translator = new FeaturesTranslator();

    @Path("getAllFeatures")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public FeaturesModel getAllFeatures()
    {
        return translator.translate(featuresDao.getAllFeatures());
    }

    @Path("addFeature")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public FeatureModel addNewProducent(@FormParam("name") String name)
    {
        FeatureModel featureModel = new FeatureModel();

        try
        {
            Features feature = featuresDao.addFeature(name);
            featureModel.setId(feature.getId());
            featureModel.setName(feature.getName());
            featureModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            featureModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            featureModel.setStatus(2);
        }

        return featureModel;
    }

    @Path("editFeature")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public FeatureModel editPhase(@FormParam("id") Integer id, @FormParam("name") String name)
    {
        FeatureModel featureModel = new FeatureModel();

        try
        {
            Features feature = featuresDao.editFeature(id, new Features(name));
            featureModel.setName(feature.getName());
            featureModel.setId(feature.getId());
            featureModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            featureModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            featureModel.setStatus(2);
        }

        return featureModel;
    }

    @Path("deleteFeature")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public FeatureModel deleteFeature(@FormParam("id") Integer id)
    {
        FeatureModel featureModel = new FeatureModel();

        try
        {
            featuresDao.deleteFeature(id);
            featureModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            featureModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            featureModel.setStatus(2);
        }

        return featureModel;
    }
}
